package cn.bx.bid.ctrl;

import cn.bx.bid.util.LogUtil;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RoleInterceptor extends HandlerInterceptorAdapter {
    //请求之前，return true:继续执行；return false

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        LogUtil.info("被拦截的方法："+handler+",时间是："+new java.util.Date());
        if(request.getSession().getAttribute("USER_LOGIN")==null){
            //回到：login.jsp
            request.setAttribute("msg","没有权限访问");
            request.getRequestDispatcher("/login.jsp").forward(request,response);
            return false;
        }

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        LogUtil.info(handler+"执行结束了");
        if(modelAndView!=null){
            LogUtil.info("视图地址："+modelAndView.getViewName()+",返回值："+modelAndView.getModelMap());
        }
       // super.postHandle(request, response, handler, modelAndView);

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        //super.afterCompletion(request, response, handler, ex);
        LogUtil.info("视图结束了");
    }
}
